/*
DynamicReplicaPlacement is an application based on the work of 
Yan Chen, H. Katz and John D. Kubiatowicz
provided at the paper named "Dynamic Replica Placement for Scalable Content Delivery"
Copyright (C) 2008  Josias Barbosa de Lima Junior, 
                    Rafael Alves Roberto,
                    Andre Vitor de Almeida Palhares. 

This file is part of DynamicReplicaPlacement. 

DynamicReplicaPlacement is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

DynamicReplicaPlacement is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/

package tests.simulation;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import p2pcdnsim.network.NetworkTopology;
import p2pcdnsim.network.TopologyConstructor;
import p2pcdnsim.report.Bandwidth;
import p2pcdnsim.report.SimReporter;
import p2pcdnsim.xml.XMLNetworkTopologyBuilder;

import desmoj.core.simulator.Experiment;
import desmoj.core.simulator.Model;
import desmoj.core.simulator.SimTime;
import desmoj.core.statistic.*;

public class DynamicTest1 extends Model {
	public static final int SIM_TIME = 80;

	public DynamicTest1(Model owner, String modelName, boolean showInReport, boolean showInTrace) {
		super(owner, modelName, showInReport, showInTrace);
	}

	public String description() {
		return "DynamicReplicaPlacement Simulator - Test1";
	}

	public void doInitialSchedules() {

		try {
			TopologyConstructor tConstructor = new TopologyConstructor();
			tConstructor.setModel(this);
			tConstructor.setBuilder(new XMLNetworkTopologyBuilder(getRouterDoc(), "100000000", "0.000", null));
			tConstructor.constructTopology();
			NetworkTopology topology = tConstructor.getNetworkTopology();
			
			topology.printInfo();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		long finishTime;

		// create model and experiment
		DynamicTest1 model = new DynamicTest1(null, "P2P Simulator", true, true);
		Experiment exp = new Experiment("Simulator");
		model.connectToExperiment(exp);

		// set experiment parameters
		exp.setShowProgressBar(true); // display a progress bar (or not)
		exp.stop(new SimTime(SIM_TIME)); // set end of simulation at 1500
		// time units
		exp.tracePeriod(new SimTime(0.0), new SimTime(1)); // set the period of
		// the trace
		exp.debugPeriod(new SimTime(0.0), new SimTime(1)); // and debug output

		exp.start();
		exp.report();
		exp.finish();

		finishTime = System.currentTimeMillis();
		long time = finishTime - startTime;

		Date date = new Date(time);
		String simulationTime = (new SimpleDateFormat("hh:mm:ss")).format(date);
		System.out.println("Simulation time: " + simulationTime);

	}

	public void init() {
		SimReporter.getInstance().registerReportable(SimReporter.NUMBER_OF_REQUESTS, new Count(this, "Number of Requests", true, false));
		SimReporter.getInstance().registerReportable(SimReporter.HIT_RATIO, new Count(this, "Hit Ratio", true, false));
		SimReporter.getInstance().registerReportable(SimReporter.MISS_RATIO, new Count(this, "Miss Ratio", true, false));		
		SimReporter.getInstance().registerReportable(SimReporter.STARTUP_DELAY, new Accumulate(this, "Startup Delay", true, false));
		SimReporter.getInstance().registerReportable(SimReporter.PACKET_LOSS, new Count(this, "Packet Loss", true, false));
		SimReporter.getInstance().registerReportable(SimReporter.BANDWIDTH_CONSUMPTION, new Bandwidth (this, "Bandwidth Consumption", true, false));
	}

	private String getScenario() {
		return "scenario1";
	}
	
	private Document getRouterDoc() throws JDOMException, IOException {
		String file = "xml/"+getScenario()+"/Routers.xml";
		SAXBuilder builder = new SAXBuilder();
		return builder.build(new File(file));
	}


}
